using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 空接口，用于字典等进行调用
/// </summary>
public interface IEvent
{
    //
}

/// <summary>
/// 有参泛型事件
/// </summary>
/// <typeparam name="T"></typeparam>
public class EventInfo<T> :IEvent
{
    public UnityAction<T> action;
    public EventInfo(UnityAction<T> action)
    {
        this.action += action;
    }
}

/// <summary>
/// 无参泛型事件
/// </summary>
public class EventInfo :IEvent
{
    public UnityAction action;
    public EventInfo(UnityAction action)
    {
        this.action += action;
    }
}

public class EventCenter : Singleton<EventCenter>
{
    //
    private Dictionary<string, IEvent> eventDic = new Dictionary<string, IEvent>();
    
    //
    /// <summary>
    /// 添加事件监听行为
    /// </summary>
    /// <param name="name">事件名字</param>
    /// <param name="action">事件监听行为</param>
    public void AddEventListener(string name, UnityAction action)
    {
        if( eventDic.ContainsKey(name) )
        {
            (eventDic[name] as EventInfo).action += action;
        }
        else
        {
            eventDic.Add(name, new EventInfo( action ));
        }
    }

    /// <summary>
    /// 移除事件监听行为
    /// </summary>
    /// <param name="name">事件名字</param>
    /// <param name="action">监听行为</param>
    public void RemoveEventListener(string name, UnityAction action)
    {
        if (eventDic.ContainsKey(name))
            (eventDic[name] as EventInfo).action -= action;
    }

    /// <summary>
    /// 添加事件监听行为
    /// </summary>
    /// <param name="name">事件名字</param>
    /// <param name="action">事件监听行为</param>
    /// <typeparam name="T">泛型</typeparam>
    public void AddEventListener<T>(string name, UnityAction<T> action)
    {
        if( eventDic.ContainsKey(name) )
        {
            (eventDic[name] as EventInfo<T>).action += action;
        }
        else
        {
            eventDic.Add(name, new EventInfo<T>( action ));
        }
    }

    /// <summary>
    /// 移除事件监听行为
    /// </summary>
    /// <param name="name">事件名字</param>
    /// <param name="action">监听行为</param>
    /// <typeparam name="T">泛型</typeparam>
    public void RemoveEventListener<T>(string name, UnityAction<T> action)
    {
        if (eventDic.ContainsKey(name))
            (eventDic[name] as EventInfo<T>).action -= action;
    }

    /// <summary>
    /// 泛型参数事件触发
    /// </summary>
    /// <param name="name">事件名字</param>
    /// <param name="info">泛型参数</param>
    /// <typeparam name="T">泛型</typeparam>
    public void EventTrigger<T>(string name, T info)
    {
        if (eventDic.ContainsKey(name))
        {
            if((eventDic[name] as EventInfo<T>).action != null)
                (eventDic[name] as EventInfo<T>).action.Invoke(info);
        }
    }

    /// <summary>
    /// 无参事件触发
    /// </summary>
    /// <param name="name">事件名字</param>
    public void EventTrigger(string name)
    {
        if (eventDic.ContainsKey(name))
        {
            if ((eventDic[name] as EventInfo).action != null)
                (eventDic[name] as EventInfo).action.Invoke();
        }
    }

    /// <summary>
    /// 清空事件中心
    /// </summary>
    public void Clear()
    {
        eventDic.Clear();
    }
}